
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Browser_counter;

public class GenericBrowserCounterDAO implements
		org.jetnuke.dao.BrowserCounterDAO {


	/* (non-Javadoc)
	 * @see org.jetnuke.model.BrowserCounterModel#incCounter(java.lang.String)
	 */
	public void incCounter(String browser) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BrowserCounterModel.incCounter"));
		p.setString(1,browser);
		p.executeUpdate();
		p.close();
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.BrowserCounterModel#getTotalCount()
	 */
	public int getTotalCount() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BrowserCounterModel.getTotalCount"));
		ResultSet rs = p.executeQuery();
		rs.next();
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.BrowserCounterModel#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BrowserCounterModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	private List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			Browser_counter u = new Browser_counter();
			fillBrowserCounterFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}

	/**
	 * @param u
	 * @param rs
	 */
	private void fillBrowserCounterFromResultSet(Browser_counter u, ResultSet rs)throws Exception {
		u.setBID(rs.getString("BID"));
		u.setCOUNTER(rs.getInt("COUNTER"));
		
	}

}
